import java.io.IOException;


public class NetworkPlayer {
	
	protected Connection m_Connection;
	
	public NetworkPlayer(Connection c) {
		m_Connection = c;
	}
	
	public void startListen(final HeliListener listener) {
		Thread t = new Thread(new Runnable() {
		
			public void run() {
				while(true) {
					try {
						handleCommand(m_Connection.readInt(), listener);
					} catch (IOException e) {
						return;
					}
				}
			}

			
		
		});
		System.out.println("starting listen");
		t.start();
	}
	
	private void handleCommand(int command, HeliListener listener) throws IOException {
		System.out.println("got int: " + command);
		switch (command) {
		case MOVE_HELICOPTER:
		{
			int direction = m_Connection.readInt();
			byte isForward = m_Connection.readByte();
			listener.moveHelicopter(direction, isForward==1);
			break;
		}	
		case STOP_HELICOPTER:
		{
			int x = m_Connection.readInt();
			int y = m_Connection.readInt();
			listener.stopHeli(x,y);
			break;
		}	
		case FIRE_ROCKET:
		{
			int dir = m_Connection.readInt();
			listener.fireRocket(dir);
			break;
		}	
		case MOVE_ROCKET:
		{
			System.out.println("GOT MOVE ROCKET!!!");
			int x = m_Connection.readInt();
			int y = m_Connection.readInt();
			int dir = m_Connection.readInt();
			listener.moveRocket(x, y, dir);
			break;
		}
		case STOP_ROCKET:
		{
			int x = m_Connection.readInt();
			int y = m_Connection.readInt();
			int dir = m_Connection.readInt();
			listener.stopRocket(x, y, dir);
			break;
		}
		default:
			break;
		}
	}
	
	/*		int	      String
	 * [INIT_PLAYERS][PLAYER1]
	 * 
	 * 			String
	 * answer: [PLAYER2]
	 */
	protected static final int INIT_PLAYERS = 2;
	
	/*		int
	 * [START_GAME]
	 */
	protected static final int START_GAME = 3;
	
	/*	   int	   int
	 * [END_GAME][Winner]
	 * 
	 * winner: 0.... client
	 * 		   1.... server
	 */
	protected static final int END_GAME = 4;
	
	/*		int				int			byte
	 * [MOVE_HELICOPTER][ORIENTATION][IS_FORWARD]
	 *
	 * is_forward: 0...backward
	 * 			   1...forward
	 */
	protected static final int MOVE_HELICOPTER = 8;
	
	/*
	 * 		int          int   int
	 * [STOP_HELICOPTER] [X]   [Y]
	 */
	protected static final int STOP_HELICOPTER = 9;
	
	/*		int			int		 int int
	 * [MOVE_ROCKET] [DIRECTION] [X] [Y]
	 * 
	 * 	direction: 0...left
	 * 			   1...right
	 */
	protected static final int MOVE_ROCKET = 10;
	
	/*		int			int		   int int
	 * [STOP_ROCKET] [ORIENTATION] [X] [Y]
	 */
	protected static final int STOP_ROCKET = 11;

	/*		int			  int
	 * [FIRE_ROCKET) [ORIENTATION]
	 */
	protected static final int FIRE_ROCKET = 12;
	
	/*		int				int			byte
	 * [MOVE_HELICOPTER][ORIENTATION][IS_FORWARD]
	 *
	 * is_forward: 0...backward
	 * 			   1...forward
	 */
	public void moveHelicopter(int orientation, byte isForward) throws IOException {
		m_Connection.writeInt(MOVE_HELICOPTER).writeInt(orientation).writeByte(isForward).flush();
	}
	
	/*
	 * 		int
	 * [STOP_HELICOPTER]
	 */
	public void stopHelicopter(int x, int y) throws IOException {
		m_Connection.writeInt(STOP_HELICOPTER).writeInt(x).writeInt(y).flush();
	}
	
	public void moveRocket(int direction, int x, int y) throws IOException {
		m_Connection.writeInt(MOVE_ROCKET).writeInt(x).writeInt(y).writeInt(direction).flush();
	}
	
	public void stopRocket(int direction, int x, int y) throws IOException {
		m_Connection.writeInt(STOP_ROCKET).writeInt(x).writeInt(y).writeInt(direction).flush();
	}
	
	public void fireRocket(int direction) throws IOException {
		m_Connection.writeInt(FIRE_ROCKET).writeInt(direction).flush();
	}
}
